package com.starinside.wp.controller;

import com.starinside.wp.foundation.model.StatusResponse;
import com.starinside.wp.utils.HttpClient;
import com.starinside.wp.utils.JsonUtil;
import io.swagger.annotations.Api;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Map;

/**
 * @author mengzhichao
 * @create 2025-02-27-9:44
 */

@RestController
@RequestMapping("/children")
@Api(tags = "宝宝模块")
public class TeachApiController {

    @Value("${teach.server.url}")
    private String crmServerURL;



    /**
     * 新增请假
     */
    @PostMapping("/saveChildrenLeaveRequest")
    public StatusResponse saveChildrenLeaveRequest(@RequestBody Map aboutPallet) throws IOException {
        String json = HttpClient.doPost(crmServerURL + "/api/web/children/saveChildrenLeaveRequest",aboutPallet);
        Map map = JsonUtil.readToMap(json);
        return StatusResponse.success(map.get("data"));
    }




    /**
     * 修改请假
     */
    @PostMapping("/editChildrenLeaveRequest")
    public StatusResponse editChildrenLeaveRequest(@RequestBody Map aboutPallet) throws IOException {
        String json = HttpClient.doPost(crmServerURL + "/api/web/children/editChildrenLeaveRequest",aboutPallet);
        Map map = JsonUtil.readToMap(json);
        return StatusResponse.success(map.get("data"));
    }





    /**
     * 请假列表查询
     */
    @GetMapping("/selChildrenLeaveRequestPage")
    public StatusResponse selChildrenLeaveRequestPage(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String chldrenId,
            @RequestParam(required = false) String classesId,
            @RequestParam(required = false) String name,
            @RequestParam(defaultValue = "0") int pageNum,  // 默认页码是1
            @RequestParam(defaultValue = "10") int pageSize) throws IOException {

        // 构建请求参数
        StringBuilder requestUrl = new StringBuilder(crmServerURL + "/api/web/children/selChildrenLeaveRequestPage");

        // 如果有查询参数，构造URL
        if (startDate != null) {
            requestUrl.append("?startDate=").append(startDate);
        }
        if (classesId != null && StringUtils.isNotEmpty(classesId)) {
            if (requestUrl.indexOf("?") == -1) {
                requestUrl.append("?classesId=").append(classesId);
            } else {
                requestUrl.append("&classesId=").append(classesId);
            }
        }
        if (chldrenId != null && StringUtils.isNotEmpty(chldrenId)) {
            if (requestUrl.indexOf("?") == -1) {
                requestUrl.append("?chldrenId=").append(chldrenId);
            } else {
                requestUrl.append("&chldrenId=").append(chldrenId);
            }
        }
        if (name != null && StringUtils.isNotEmpty(name)) {
            if (requestUrl.indexOf("?") == -1) {
                requestUrl.append("?name=").append(name);
            } else {
                requestUrl.append("&name=").append(name);
            }
        }
        // 添加分页参数
        if (requestUrl.indexOf("?") == -1) {
            requestUrl.append("?pageNum=").append(pageNum);
        } else {
            requestUrl.append("&pageNum=").append(pageNum);
        }
        requestUrl.append("&pageSize=").append(pageSize);

        System.out.println("远程调用接口：+++++++++++++" +requestUrl.toString());
        String json = HttpClient.doGet(requestUrl.toString());
        Map map = JsonUtil.readToMap(json);
        return StatusResponse.success(map.get("data"));
    }







    /**
     * 新增疫苗
     */
    @PostMapping("/addChildrenVaccinesOne")
    public StatusResponse addChildrenVaccinesOne(@RequestBody Map aboutPallet) throws IOException {
        String json = HttpClient.doPost(crmServerURL + "/api/web/children/addChildrenVaccinesOne",aboutPallet);
        Map map = JsonUtil.readToMap(json);
        String code = (String) map.get("code");
        if (code.equals("500")){
            return StatusResponse.error("500","已接种该疫苗");
        }
        return StatusResponse.success(map.get("data"));
    }






    /**
     * 疫苗列表查询
     */
    @GetMapping("/selChildrenVaccinesPage")
    public StatusResponse selChildrenVaccinesPage(
            @RequestParam(required = false) String classesId,
            @RequestParam(required = false) String chldrenId,
            @RequestParam(required = false) Integer type,
            @RequestParam(required = false) Integer state,
            @RequestParam(required = false) String name,
            @RequestParam(defaultValue = "0") int pageNum,  // 默认页码是1
            @RequestParam(defaultValue = "10") int pageSize) throws IOException {

        // 构建请求参数
        StringBuilder requestUrl = new StringBuilder(crmServerURL + "/api/web/children/selChildrenVaccinesPage");

        // 如果有查询参数，构造URL
        if (classesId != null && StringUtils.isNotEmpty(classesId)) {
            if (requestUrl.indexOf("?") == -1) {
                requestUrl.append("?classesId=").append(classesId);
            } else {
                requestUrl.append("&classesId=").append(classesId);
            }
        }
        if (chldrenId != null && StringUtils.isNotEmpty(chldrenId)) {
            if (requestUrl.indexOf("?") == -1) {
                requestUrl.append("?chldrenId=").append(chldrenId);
            } else {
                requestUrl.append("&chldrenId=").append(chldrenId);
            }
        }

        if (type!=null) {
            if (requestUrl.indexOf("?") == -1) {
                requestUrl.append("?type=").append(type);
            } else {
                requestUrl.append("&type=").append(type);
            }
        }

        if (state!=null) {
            if (requestUrl.indexOf("?") == -1) {
                requestUrl.append("?state=").append(state);
            } else {
                requestUrl.append("&state=").append(state);
            }
        }

        if (name != null && StringUtils.isNotEmpty(name)) {
            if (requestUrl.indexOf("?") == -1) {
                requestUrl.append("?name=").append(name);
            } else {
                requestUrl.append("&name=").append(name);
            }
        }
        // 添加分页参数
        if (requestUrl.indexOf("?") == -1) {
            requestUrl.append("?pageNum=").append(pageNum);
        } else {
            requestUrl.append("&pageNum=").append(pageNum);
        }
        requestUrl.append("&pageSize=").append(pageSize);

        System.out.println("远程调用接口：+++++++++++++" +requestUrl.toString());
        String json = HttpClient.doGet(requestUrl.toString());
        Map map = JsonUtil.readToMap(json);
        return StatusResponse.success(map.get("data"));
    }




}
